using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ClientTools.Client
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private UserControl _ActControl = null;
        private global::ClientTools.Client.Library.Settings.Profile _ActProfile = null;
        private bool _bSetData = false;
        private Client.Controls.ControlInfoCollection _ControlInfos = null;
        //private Client.Controls.ControlInfoCollection _ControlInfosNoProfile = null;

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text += Application.ProductVersion.Substring(0, Application.ProductVersion.LastIndexOf("."));
            this._bSetData = true;
            this.FillProfiles();
            this._bSetData = false;
            this.cboProfiles.SelectedIndex = 0;

            _ControlInfos = Client.Controls.ControlsManager.GetAllUserControls(Application.StartupPath);
            //_ControlInfosNoProfile = Client.Controls.ControlsManager.GetAllUserControlsNoProfile(Application.StartupPath);
            this.FillButtons();
        }

        private void FillButtons()
        {
            int addIndex = 0;

            for (int i = 0; i < _ControlInfos.Count; i++)
            {
                Client.Controls.ControlInfo ci = _ControlInfos[i];

                if (ci.ShowDelimiter && addIndex > 0)
                {
                    ToolStripSeparator tss = new ToolStripSeparator();
                    this.ToolStrip1.Items.Insert(addIndex++, tss);
                }

                ToolStripItem ti = ToolStrip1.Items.Add(ci.ButtonText);
                this.ToolStrip1.Items.Insert(addIndex++, ti);
                ti.Tag = ci;
                ti.Image = ci.ButtonImage;
                ti.ImageTransparentColor = System.Drawing.Color.Magenta;
                ti.Name = "tsb" + ci.TypeName;
                ti.Padding = new System.Windows.Forms.Padding(5);
                ti.Size = new System.Drawing.Size(68, 43);
                ti.Text = ci.ButtonText;
                ti.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
                ti.Click += new System.EventHandler(this.tsbUserControl_Click);
            }
            //if (addIndex > 0)
            //{
            //    ToolStripSeparator tss = new ToolStripSeparator();
            //    this.ToolStrip1.Items.Insert(addIndex++, tss);
            //    tss = new ToolStripSeparator();
            //    this.ToolStrip1.Items.Insert(addIndex++, tss);
            //}
            //for (int i = 0; i < _ControlInfosNoProfile.Count; i++)
            //{
            //    Client.Controls.ControlInfo ci = _ControlInfosNoProfile[i];

            //    if (ci.ShowDelimiter && i > 0)
            //    {
            //        ToolStripSeparator tss = new ToolStripSeparator();
            //        this.ToolStrip1.Items.Insert(addIndex++, tss);
            //    }

            //    ToolStripItem ti = ToolStrip1.Items.Add(ci.ButtonText);
            //    this.ToolStrip1.Items.Insert(addIndex++, ti);
            //    ti.Tag = ci;
            //    ti.Image = ci.ButtonImage;
            //    ti.ImageTransparentColor = System.Drawing.Color.Magenta;
            //    ti.Name = "tsb" + ci.TypeName;
            //    ti.Padding = new System.Windows.Forms.Padding(5);
            //    ti.Size = new System.Drawing.Size(68, 43);
            //    ti.Text = ci.ButtonText;
            //    ti.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            //    ti.Click += new System.EventHandler(this.tsbUserControl_Click);
            //}
        }

        private void FillProfiles()
        {
            this.cboProfiles.Items.Clear();
            foreach (Library.Settings.Profile var in Globals.AppSettings.Profiles)
            {
                this.cboProfiles.Items.Add(var.ProfileName);
            }
        }

        private void ShowUserControl(UserControl uc)
        {
            this.Cursor = Cursors.WaitCursor;
            Type t = uc.GetType();

            if (this._ActControl != null)
            {
                if (this._ActControl.GetType() == typeof(Client.Controls.Settings.ucSettings))
                {
                    ((Client.Controls.Settings.ucSettings)this._ActControl).ProfileSavedEvent -= this.ProfileSaved;
                    ((Client.Controls.Settings.ucSettings)this._ActControl).ProfileDeletedEvent -= this.ProfileDeleted;
                    ((Client.Controls.Settings.ucSettings)this._ActControl).NewProfileAddedEvent -= this.NewProfileAdded;
                }
                this._ActControl.Hide();
                this.pnlMain.Controls.Clear();
                this._ActControl = null;
            }
            this._ActControl = uc;
            this.pnlMain.Controls.Add(this._ActControl);
            this._ActControl.Dock = DockStyle.Fill;

            if (t == typeof(Client.Controls.Settings.ucSettings))
            {
                ((Client.Controls.Settings.ucSettings)this._ActControl).ProfileSavedEvent += this.ProfileSaved;
                ((Client.Controls.Settings.ucSettings)this._ActControl).ProfileDeletedEvent += this.ProfileDeleted;
                ((Client.Controls.Settings.ucSettings)this._ActControl).NewProfileAddedEvent += this.NewProfileAdded;
            }

            if (t.GetInterface(typeof(Client.Controls.IUserControl).Name) != null)
            {
                ((Client.Controls.IUserControl)this._ActControl).SetData(_ActProfile);
                ((Client.Controls.BaseUserControl)this._ActControl).Show();
                this.lblHeader.Text = ">> " + ((Client.Controls.IUserControl)this._ActControl).Header + " <<";
                this.pnlProfile.Visible = true;
            }
            //else
            //{
            //    if (t.GetInterface(typeof(Client.Controls.IUserControlNoProfile).Name) != null)
            //    {
            //        ((Client.Controls.IUserControlNoProfile)this._ActControl).SetData();
            //        this._ActControl.Show();
            //        this.lblHeader.Text = ">> " + ((Client.Controls.IUserControlNoProfile)this._ActControl).Header + " <<";
            //        this.pnlProfile.Visible = false;
            //    }
            //}

            this.Cursor = Cursors.Default;
        }

        private void ProfileSaved()
        {
            Globals.SaveSettings();
            if (this._ActProfile != null && this._ActProfile.ProfileName != (string)this.cboProfiles.SelectedItem)
            {
                this._bSetData = true;
                this.FillProfiles();
                this.cboProfiles.SelectedItem = this._ActProfile.ProfileName;
                this._bSetData = false;
            }
        }

        private void ProfileDeleted()
        {
            this._bSetData = true;

            Globals.AppSettings.DeleteProfile(this._ActProfile);
            this._ActProfile = null;

            this.FillProfiles();
            if (Globals.AppSettings.Profiles.Count == 0)
            {
                this._ActProfile = null;
            }
            else
            {
                this._ActProfile = Globals.AppSettings.Profiles[0];
                this.cboProfiles.SelectedItem = this._ActProfile.ProfileName;
            }
            this._bSetData = false;
            this.SetProfile();
        }

        private void NewProfileAdded()
        {
            this._bSetData = true;
            this._ActProfile = Globals.AppSettings.AddNewProfile(); ;
            this.FillProfiles();
            this.cboProfiles.SelectedItem = this._ActProfile.ProfileName;
            this._bSetData = false;
            this.SetProfile();
        }

        private void tsbUserControl_Click(object sender, EventArgs e)
        {
            this.ShowUserControl((System.Windows.Forms.UserControl)Activator.CreateInstance(((Client.Controls.ControlInfo)((ToolStripButton)sender).Tag).Type));
        }

        private void tsbAbout_Click(object sender, EventArgs e)
        {
            this.ShowUserControl(new Client.Controls.ucAbout());
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SetProfile()
        {
            if (Globals.AppSettings.Profiles.Count == 0)
            {
                this._ActProfile = null;
            }
            else
            {
                this._ActProfile = Globals.AppSettings.Profiles[this.cboProfiles.SelectedIndex];
            }
            if (this._ActControl != null)
            {
                ((Controls.IUserControl)this._ActControl).SetData(this._ActProfile);
            }
        }

        private void cboProfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!this._bSetData)
            {
                this.SetProfile();
            }
        }

    }
}